package com.b2b.util;

import com.b2b.model.*;
import com.b2b.model.entity.*;
import com.b2b.service.InquiryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class InquiryUtil {
    private final InquiryService inquiryService;
    private final RequirementOfMaterialUtil requirementOfMaterialUtil;
    private final InquiryMaterialUtil inquiryMaterialUtil;
    private final CompanyInfoUtil companyInfoUtil;
    private final ReceivingInfoUtil receivingInfoUtil;
    @Autowired
    public InquiryUtil(InquiryService inquiryService,RequirementOfMaterialUtil requirementOfMaterialUtil,InquiryMaterialUtil inquiryMaterialUtil
            ,CompanyInfoUtil companyInfoUtil,ReceivingInfoUtil receivingInfoUtil
    ){
        this.inquiryService = inquiryService;
        this.requirementOfMaterialUtil = requirementOfMaterialUtil;
        this.inquiryMaterialUtil = inquiryMaterialUtil;
        this.companyInfoUtil = companyInfoUtil;
        this.receivingInfoUtil = receivingInfoUtil;
    }

    public Integer insertInquiryList(InquiryListDto inquiryListDto){
        Integer inquiryId = insertInquiry(getInquiry(inquiryListDto));
        List<RequirementOfMaterial> requirementOfMaterialList = inquiryListDto.getRequirementList();
        InquiryReceiving inquiryReceiving = new InquiryReceiving();
        inquiryReceiving.setInquiryId(inquiryId);
        inquiryReceiving.setReceivingInfoId(inquiryListDto.getReceivingInfoId());
        Integer result = null;
        if (inquiryId <= 0) {
            result = -1;
            return result;
        } else {
            for (int i = 0; i < requirementOfMaterialList.size(); i++) {
                RequirementOfMaterial requirementOfMaterial = requirementOfMaterialList.get(i);
                Integer requirementId = requirementOfMaterialUtil.insertRequirementOfMaterial(requirementOfMaterial);
                if (requirementId>=0){
                    InquiryMaterial inquiryMaterial = new InquiryMaterial();
                    inquiryMaterial.setInquiryId(inquiryId);
                    inquiryMaterial.setRequirementId(requirementId);
                    result = inquiryMaterialUtil.insertInquiryMaterial(inquiryMaterial);
                }else {
                    result = -1;
                }
            }
            if ( result >=0){
                return inquiryId;
            }else {
                return result;
            }
        }
    }
    public Integer insertInquiry(Inquiry inquiry){
        int count = inquiryService.insert(inquiry);
        if (count<=0){
            Integer Id = -1;
            return Id;
        }else {
            Integer Id = inquiry.getInquiryId();
            return Id;
        }
    }
    public String getInquiryTitle(Integer inquiryId){
        Inquiry inquiry = inquiryService.selectByPrimaryKey(inquiryId);
        String inquiryTitle = inquiry.getInquiryTitle();

        return inquiryTitle;
    }
    public Inquiry getInquiryByInquiryId(Integer inquiryId){
        Inquiry inquiry = new Inquiry();
        inquiry.setInquiryId(inquiryId);
        inquiry = inquiryService.selectByPrimaryKey(inquiry);
        return inquiry;
    }
    public Map<String,Object> selectByInquiry(Integer inquiryId){
        Inquiry inquiry = new Inquiry();
        inquiry.setInquiryId(inquiryId);
        inquiry = inquiryService.selectByPrimaryKey(inquiry);
        Integer receivingInfoId = inquiry.getReceivingInfoId();
        Map<String,Object> inquiryReceiving = new HashMap<>();
        inquiryReceiving.put("inquiryInfo",inquiry);
        ReceivingInfo receivingInfo = receivingInfoUtil.selectReceivingInfo(receivingInfoId);
        inquiryReceiving.put("buyerReceivingInfo",receivingInfo);
        return inquiryReceiving;
    }
    public Integer deleteInquiry(Integer inquiryId){
        Inquiry inquiry = new Inquiry();
        inquiry.setInquiryId(inquiryId);
        InquiryMaterial inquiryMaterial = new InquiryMaterial();
        inquiryMaterial.setInquiryId(inquiryId);
        List<InquiryMaterial> inquiryMaterialList = inquiryMaterialUtil.getInquiryMaterialByInquiryId(inquiryMaterial.getInquiryId());
        for (int i = 0; i < inquiryMaterialList.size(); i++) {
            InquiryMaterial inquiryMaterial1 = inquiryMaterialList.get(i);
            requirementOfMaterialUtil.deleteInquiry(inquiryMaterial1.getRequirementId());
        }
        for (int i = 0; i < inquiryMaterialList.size(); i++) {
            InquiryMaterial inquiryMaterial1 = inquiryMaterialList.get(i);
            inquiryMaterialUtil.deleteInquiryMaterial(inquiryMaterial1);
        }
        Integer count = inquiryService.deleteByPrimaryKey(inquiry);
        return count;
    }

    public Integer updateInquiry(Inquiry inquiry){
        Integer count = inquiryService.updateByPrimaryKeySelective(inquiry);
        return count;
    }

    public List<Inquiry> getAll(){
        List<Inquiry> inquiryList = inquiryService.selectAll();
        return inquiryList;
    }

    public List<InquiryRoughDto> getAllRough(){
        List<InquiryRoughDto> inquiryRoughDtoList = getRough(getAll());

        return inquiryRoughDtoList;
    }

    public List<InquiryRoughDto> getByCondition(InquiryConditionDto conditionDto){
        Inquiry inquiry = new Inquiry();
        if (conditionDto.getInquiryDeadline()==null&&conditionDto.getInquiryTitle().equals("")&&conditionDto.getInquiryTime()==null){
            List<Inquiry> inquiryList = inquiryService.selectAll();
            List<InquiryRoughDto> inquiryRoughDtoList = getRough(inquiryList);

            return inquiryRoughDtoList;
        }else {
            inquiry.setInquiryTitle(conditionDto.getInquiryTitle());
            inquiry.setInquiryTime(conditionDto.getInquiryTime());
            inquiry.setInquiryDeadline(conditionDto.getInquiryDeadline());
            List<Inquiry> inquiryList = inquiryService.select(inquiry);
            inquiryList = getListByMaterialName(inquiryList, conditionDto.getMaterialName());
            List<InquiryRoughDto> inquiryRoughDtoList = getRough(inquiryList);

            return inquiryRoughDtoList;
        }
    }
    public List<Inquiry> getAllByC(Integer companyId){
        Inquiry inquiry = new Inquiry();
        inquiry.setCompanyId(companyId);
        List<Inquiry> inquiryList = inquiryService.select(inquiry);
        return inquiryList;
    }


    public List<InquiryRoughDto> getRoughByC(Integer companyId){
        List<InquiryRoughDto> inquiryRoughDtoList = getRough(getAllByC(companyId));
        return inquiryRoughDtoList;
    }
    public List<InquiryRoughDto> getRough(List<Inquiry> inquiryList){
        List<InquiryRoughDto> inquiryRoughDtoList = new ArrayList<InquiryRoughDto>();
        for (int i = 0; i < inquiryList.size(); i++) {
            InquiryRoughDto inquiryRoughDto = getInquiryRoughDto(inquiryList.get(i));
            inquiryRoughDtoList.add(inquiryRoughDto);
        }
        return inquiryRoughDtoList;
    }

    public List<Inquiry> getListByMaterialName(List<Inquiry> inquiryList,String materialName){
        List<Inquiry> inquiries = new ArrayList<Inquiry>();
        for (int i = 0; i < inquiryList.size(); i++) {
            List<RequirementOfMaterial> requirementOfMaterialList = requirementOfMaterialUtil.getAll(inquiryList.get(i).getInquiryId());
            requirementOfMaterialList = requirementOfMaterialList.stream().filter(requirementOfMaterial -> requirementOfMaterial.getMaterialName().equals(materialName)).collect(Collectors.toList());
            if (requirementOfMaterialList.size()>0){
                inquiries.add(inquiryList.get(i));
            }
//            for (int j = 0; j < requirementOfMaterialList.size(); j++) {
//                RequirementOfMaterial requirementOfMaterial = requirementOfMaterialList.get(j);
//                if (requirementOfMaterial.getMaterialName().equals(materialName)){
//                    inquiries.add(inquiryList.get(i));
//                    break;
//                }
//            }
        }
        return inquiries;
    }
    public InquiryRoughDto getInquiryRoughDto(Inquiry inquiry){
        InquiryRoughDto inquiryRoughDto = new InquiryRoughDto();

        inquiryRoughDto.setInquiryId(inquiry.getInquiryId());
        inquiryRoughDto.setInquiryTitle(inquiry.getInquiryTitle());
        inquiryRoughDto.setInquiryDeadline(inquiry.getInquiryDeadline());
        inquiryRoughDto.setInquiryTime(inquiry.getInquiryTime());
        inquiryRoughDto.setCompanyId(inquiry.getCompanyId());
        inquiryRoughDto.setCompanyName(companyInfoUtil.getCompanyName(inquiry.getCompanyId()));
        List<RequirementOfMaterial> requirementOfMaterialList = requirementOfMaterialUtil.getAll(inquiry.getInquiryId());
        List<String> requirementOfMaterials = new ArrayList<String>();
        if (requirementOfMaterialList.size()<=5){
            for (int i = 0; i < requirementOfMaterialList.size(); i++) {
                String companyName = requirementOfMaterialList.get(i).getMaterialName();
                requirementOfMaterials.add(companyName);
            }
        }else {
            for (int i = 0; i < 5; i++) {
                String companyName = requirementOfMaterialList.get(i).getMaterialName();
                requirementOfMaterials.add(companyName);
            }
        }
        inquiryRoughDto.setRequirementOfMaterialDtos(requirementOfMaterials);
        return inquiryRoughDto;
    }
    public Inquiry getInquiry(InquiryListDto inquiryListDto){
        Inquiry inquiry = new Inquiry();
        inquiry.setInquiryTime(new Date());
        inquiry.setInquiryTitle(inquiryListDto.getInquiryTitle());
        inquiry.setCompanyId(inquiryListDto.getCompanyId());
        inquiry.setInquiryDeadline(inquiryListDto.getInquiryDeadline());
        inquiry.setTaxIncluded(inquiryListDto.getTaxIncluded());
        inquiry.setFreightIncluded(inquiryListDto.getFreightIncluded());
        inquiry.setPartialMaterial(inquiryListDto.getPartialMaterial());
        inquiry.setPartialNumber(inquiryListDto.getPartialNumber());
        inquiry.setDateOfDelivery(inquiryListDto.getDateOfDelivery());
        inquiry.setExpectedDate(inquiryListDto.getExpectedDate());
        inquiry.setPriceValidity(inquiryListDto.getPriceValidity());
        inquiry.setVisibilityOfReceivingInfo(inquiryListDto.getVisibilityOfReceivingInfo());
        inquiry.setTransactionMode(inquiryListDto.getTransactionMode());
        inquiry.setPayTime(inquiryListDto.getPayTime());
        inquiry.setPaymentMode(inquiryListDto.getPaymentMode());
        inquiry.setSellerAddress(inquiryListDto.getSellerAddress());
        inquiry.setSellerRegisteredCapital(inquiryListDto.getSellerRegisteredCapital());
        inquiry.setNote(inquiryListDto.getNote());
        inquiry.setEnclosureUrl(inquiryListDto.getEnclosureUrl());
        inquiry.setPriceComparisonMethod(inquiryListDto.getPriceComparisonMethod());
        inquiry.setInternalVisibility(inquiryListDto.getInternalVisibility());
        inquiry.setNDA(inquiryListDto.getNDA());
        inquiry.setReceivingInfoId(inquiryListDto.getReceivingInfoId());

        return inquiry;
    }
}
